using System;
using System.Globalization;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.SharePoint.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel;



namespace $safeprojectname$.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("c2c86185-1fe3-4e47-b812-ee3b96fc8bfa")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
         internal static class Constants
    {
        internal const string WebPartZoneID1 = "$Left5$";
        internal const string WebPartZoneID2 = "$Right2$";
        internal const string DefaultPage = "default.aspx";
        
     
    }

        [Microsoft.SharePoint.Security.SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
           
            if (properties == null)
            {
                throw new System.ArgumentNullException("properties");
            }

            base.FeatureActivated(properties);

            try
            {
                SPWeb centerWeb = (SPWeb)properties.Feature.Parent;
                uint currentLCID = centerWeb.Language;

                //Utility.AddWebFeature(ref centerWeb, new Guid(Constants.ReportLibProvisionerFeatureId));
                //Utility.AddWebFeature(ref centerWeb, new Guid(Constants.ProductInstanceFeatureId));

                // Set site description
                centerWeb.Description = " instance site description";

                SPFile file = centerWeb.GetFile(SPUrlUtility.CombineUrl(centerWeb.Url, Constants.DefaultPage));

                
                using (SPLimitedWebPartManager webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
                {
                    string imContactsListTitle = "IM Contacts";

                    SPList imContactsList = centerWeb.Lists[imContactsListTitle]; // IM list created from element manifest of dependent feature

                    XsltListViewWebPart initialIMWebPart = GetXsltListViewWebPart(webPartManager, imContactsListTitle);

                    if (null != initialIMWebPart)
                    {
                        webPartManager.DeleteWebPart(initialIMWebPart);
                    }

                    XsltListViewWebPart imContactsListWebPart = CreateXsltListViewWebPart(imContactsList, imContactsListTitle);

                    webPartManager.AddWebPart(imContactsListWebPart, Constants.WebPartZoneID2, 2);

              
                    //'Team Discussion' Web part replace.

                    string teamDiscussionListTitle = "Team Discussion";

                    SPList teamDiscussionList = centerWeb.Lists[teamDiscussionListTitle];

                    XsltListViewWebPart initialTDWebPart = GetXsltListViewWebPart(webPartManager, teamDiscussionListTitle);

                    if (initialTDWebPart != null)
                    {
                        webPartManager.DeleteWebPart(initialTDWebPart);
                    }

                    XsltListViewWebPart teamDiscussionListWebPart = new XsltListViewWebPart();

                    teamDiscussionListWebPart.Title = teamDiscussionListTitle;
                    teamDiscussionListWebPart.ListName = teamDiscussionList.ID.ToString("B").ToUpper(CultureInfo.InvariantCulture);

                    //ZoneIndex 20 is chosen arbitrarily large. If the number of webparts in the zone increase more than this number, this needs to be changed
                    webPartManager.AddWebPart(teamDiscussionListWebPart, Constants.WebPartZoneID1, 20);

               
                    file.Update();
                };

                centerWeb.Update();
                }
            catch (Exception e)
            {
                throw;
            }

        }



        internal static XsltListViewWebPart CreateXsltListViewWebPart(SPList list, string listTitle)
        {

            XsltListViewWebPart listWebPart = new XsltListViewWebPart();

            listWebPart.Title = listTitle;
            listWebPart.ListName = list.ID.ToString("B").ToUpper(CultureInfo.InvariantCulture);
            listWebPart.ViewGuid = list.Views[0].ID.ToString();
            return listWebPart;
        }

        static XsltListViewWebPart GetXsltListViewWebPart(SPLimitedWebPartManager webPartManager, string listTitle)
        {
            XsltListViewWebPart listViewWebPart = null;

            foreach (Microsoft.SharePoint.WebPartPages.WebPart xlvwp in webPartManager.WebParts)
            {
                if (String.Equals(xlvwp.Title, listTitle))
                {
                    listViewWebPart = (XsltListViewWebPart)xlvwp;
                    break;
                }
            }
            return listViewWebPart;
        }
        
          

                                                  
 
     // Uncomment the method below to handle the event raised after a feature has been activated.

        //public override void FeatureActivated(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
